package org.zezs.sync.mongo.dataProess;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zezs.sync.mongo.Constants;
import org.zezs.sync.mongo.RunTimeUtil;
import org.zezs.sync.mongo.config.sync.metadata.Chains;
import org.zezs.sync.mongo.config.sync.metadata.Mappings;

import java.util.*;


public class MyLoggerProesser implements Proesser {
    Logger logger = LoggerFactory.getLogger(MyLoggerProesser.class);
    //需要提取的字段
    Set<String> exes = new HashSet<>();
    {
        exes.add("_id");
        exes.add("_class");
        exes.add("visitDate");
        exes.add("requestParameter");
        exes.add("status");
        exes.add("price");
        exes.add("returnParam");
        exes.add("consumeTime");
        exes.add("is_charge");
        exes.add("userMoney");
    }

    //不需要提取的字段
    Set<String> paramexes = new HashSet<>();

    {
        paramexes.add("ip");
        paramexes.add("format");
        paramexes.add("isCharge");
        paramexes.add("userMoney");
        paramexes.add("photo");

    }

    public List<JSONObject> proess(Chains chains, List<Document> documents) {
        List<JSONObject> results = new ArrayList<>();
        documents.forEach(document -> {
            Map<String, String> contentMap = new HashMap<>();

            document.entrySet().forEach(stringObjectEntry -> {
                if (!exes.contains(stringObjectEntry.getKey())) {
                    contentMap.put(stringObjectEntry.getKey(), String.valueOf(stringObjectEntry.getValue()));
                }
            });
            JSONObject req=new JSONObject();
            try{
                req = JSON.parseObject(document.getString("requestParameter"));
            }catch (Exception e){
                logger.error("json 解析失败 {}", document.toJson());
            }
            req.entrySet().forEach(stringObjectEntry -> {
                if (!paramexes.contains(stringObjectEntry.getKey())) {
                    contentMap.put(stringObjectEntry.getKey(), String.valueOf(stringObjectEntry.getValue()));
                }
            });

            JSONObject rsp=new JSONObject();
            try{
                 rsp = JSON.parseObject(document.getString("returnParam"));
            }catch (Exception e){
                logger.error("json 解析失败 {}", document.toJson());
            }
            if (rsp.get("data") != null) {
                Object data = rsp.get("data");
                if (data instanceof JSONObject)
                    ((JSONObject) data).entrySet().forEach(stringObjectEntry -> {
                        if (stringObjectEntry.getValue() != null && !(stringObjectEntry.getValue() instanceof JSON)) {
                            String string = ((JSONObject) data).getString(stringObjectEntry.getKey());
                            if (string.length() < 40) {
                                contentMap.put(stringObjectEntry.getKey(), string);
                            }
                        }
                    });

            }

            contentMap.values();
            String join = StringUtils.join(contentMap.values(), " ");

            JSONObject object = new JSONObject();
            object.put("data", document);
            object.put("content", join);
            List<Mappings> mappings = chains.getMappings();


            for (int i = 0; i < mappings.size(); i++) {
                Mappings mappings2 = mappings.get(i);
                String sourceField = mappings2.getSourceField();
                String mapppingKey = mappings2.getDestField() == null ? mappings2.getSourceField()
                        : mappings2.getDestField();
                Object mappingValue = RunTimeUtil.getDest(mappings2, sourceField == null ? null : document.get(sourceField));
                object.put(mapppingKey, mappingValue);
            }
            object.put(Constants.mongoId, document.getObjectId("_id").toString());

            results.add(object);


        });
        return results;
    }
}
